const pool = require('../config/database');

class FeedbackController {
  async submit(ctx) {
    const { content } = ctx.request.body;
    const userId = ctx.state.user.id;

    try {
      if (!content || !content.trim()) {
        ctx.status = 400;
        ctx.body = { error: "反馈内容不能为空" };
        return;
      }

      const connection = await pool.getConnection();
      try {
        await connection.execute(
          'INSERT INTO feedback (user_id, content) VALUES (?, ?)',
          [userId, content]
        );

        ctx.body = {
          ok: true,
          msg: "反馈提交成功"
        };
      } finally {
        connection.release();
      }
    } catch (error) {
      console.error("提交反馈失败:", error);
      ctx.status = 500;
      ctx.body = { error: "提交反馈失败" };
    }
  }
}

module.exports = new FeedbackController();